class bluetooth {

	constructor() {
		this.blueDeviceList = []// 搜索到的蓝牙设备列表
		this.deviceId = 'D5:13:D3:80:05:EE'
		this.serviceId = '00000001-0000-1000-8000-00805F9B34FB'
		this.characteristicId = '7E9AE1F4-40C2-8587-5D69-174769A40054'
		this.messageHex = ''
		this.message = ''
		this.redId = '00000003-0000-1000-8000-00805f9b34fb'
		this.notfiyid = '00000003-0000-1000-8000-00805F9B34FB'
	}

	hexToDec(hex) {
		try{
			hex=hex.replace(/0x/g,"")
		}catch(e){
			//TODO handle the exception
		}
		return parseInt(hex, 16)
	}
	codeToL(code){
		let c=parseInt(code,16)
		let text=c.toString(2)
		let er= text.substring(text.length-8,text.length)
		return parseInt(er,2)
	}
	hexFixToDec(data){
		if (data.substring(0,1)==8) {
			console.log('负数');
			return false
		}else{
			return true
		}
	}
	fuNumToDoc(text){
		return  text.substring(1,2)
	}
	tepHandle(data){
		let xiao =data.toString().substring(0,2)
		let pad=Number(xiao).toString(2).padStart(8,'0')
			let arr =pad.split('')
			// arr[0]=
		return
	}
	//清除数据
	cleanData1() {
		this.send([0x55, 0xaa, 0x7, 0x2, 0x1, 0x7, 0x13, 0x9, 0x8, 0x8a, 0x37, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
	}
	
	//2.3、温湿度报警数据上报回复
	getALrmReply(){
		let a=[0x55, 0xaa, 0x04, 0x32, 0x00, 0x01, 0x01, 0x34, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
		
		this.send(a)
	}
	//8、历史数据 APP 接收回复
	getHisReply(){
		let a=[0x55, 0xaa, 0x04, 0x0A, 0x00, 0x01, 0x01, 0x0c, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
		console.log(a);
		this.send(a)
	}
	//2.5、温湿度上下限阈值数据、亮度、背光灯常亮开关、报警开关、温湿度校准值数值读取
	getAlarmData(){
		this.send([0x55, 0xaa, 0x01, 0x52, 0x52, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
	}
	//6、历史数据启动上报
	getHisData(){
		this.send([0x55, 0xaa, 0x03, 0x05, 0xff, 0xff, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])
	}
	//温湿度实时数据接收启动或停止
	setListen(type){
		let code=0
		if (type=='0x00') {//关闭实时温度湿度
			code='0x04'
		}
		if (type=='0x01') {//开启实时温度湿度
			code='0x05'
		}
		let a=[0x55, 0xaa, 0x02, 0x04, type, code, 0xff, 0xff, 0xff,0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
		console.log(a);
		this.send(a)
	}
	//2.1、温湿度上下限报警数据读取
	getAlarm(){
		let a=[0x55, 0xaa, 0x03, 0x12, 0xff, 0xff, 0x10, 0xff, 0xff,0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
		// this.listenHandle(a)
		
		this.send(a)
		
	}
	//2、温湿度上下限报警、亮度、背光灯常亮开关、报警数据开关设置

	setAlarm(alarmData){
		let code =2
		let  type='0x01'
		if(alarmData.type=='h'){
			type=1
		}
		if(alarmData.type=='l'){
			type=0
		}
		if(alarmData.type=='u'){
			type=2
		}
		//转成10进制
		
		let isFuH=false
		let temperature=0
		if (Number(alarmData.temperature)>=0) {
			temperatureList = alarmData.temperature.split('.')
			if (temperatureList.length == 1) {
				temperatureList.push(0)
			}
		}else{
			//处理负数
			temperature=alarmData.temperature.replace(/\-/g,"")
			console.log(alarmData.temperature);
			temperatureList =temperature.split('.')
			isFuH=true
			if (temperatureList.length == 1) {
				temperatureList.push(0)
			}
		}
		console.log(temperatureList);
		code += Number(temperatureList[0])
		temperatureList[0]= '0x' + (Number(temperatureList[0]).toString(16).padStart(2, '0'))
		if (isFuH) {
			temperatureList[1] = '0x8' + (Number(temperatureList[1]) .toString(16))
		}else{
			temperatureList[1] = '0x' + (Number(temperatureList[1]) .toString(16).padStart(2, '0'))
		}
		code=code+this.hexToDec(temperatureList[1] )
		console.log(temperatureList);
		let humidityList=alarmData.humidity.split('.')
		if (humidityList.length == 1) {
			humidityList.push(0)
		}
		// alarmData. luminance=alarmData.luminance.replace(/\./g,"")
		// 亮度报警设置
		let  arr=['0','0']
		if (alarmData.status) {
			arr[0]='1'
		}else{
			arr[0]='0'
		}
		if (alarmData.luminanceStatus) {
			arr[1]='1'
		}else{
			arr[1]='0'
		}
		//亮度设置
		let  luminance=Number(alarmData.luminance).toString(2).padStart(6,'0').split('')
		arr=arr.concat(luminance)
		
		arr=Number(arr.join(''))
		
		arr= parseInt(arr,2)
		
		//计算加和值
		
		code=code +type
		code =code+Number(humidityList[0])+Number(humidityList[1])+arr
		
		
		code=this.codeToL(code.toString(16)).toString(16)

		let humidity1=Number(humidityList[0]).toString(16)
		let humidity2=Number(humidityList[1]).toString(16)
		type=type.toString(16).padStart(2,'0')
		let a = ['0x55', '0xaa', '0x07', '0x02','0x'+type, temperatureList[0],  temperatureList[1],  '0x'+humidity1,  '0x'+humidity2, '0x'+arr.toString(16), '0x'+code, '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff']
		console.log(a);
		this.send(a)
	
	}
	getClick(){
		let a=[0x55, 0xaa, 0x01, 0x16, 0x16, 0x00, 0xff, 0xff, 0xff,0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
		this.send(a)
	}
	setClock(weekList) {
		let code =0
		let clock1 = []
		let clock2 = []
		let clock3 = []
		for (var i = 0; i < 7; i++) {
			if (weekList[0].week[i].checked) {
				clock1.unshift(1)
			} else {
				clock1.unshift(0)
			}
			if (weekList[1].week[i].checked) {
				clock2.unshift(1)
			} else {
				clock2.unshift(0)
			}
			if (weekList[2].week[i].checked) {
				clock3.unshift(1)
			} else {
				clock3.unshift(0)
			}
		}

		if (weekList[0].checked) {
			clock1.unshift(1)
		} else {
			clock1.unshift(0)
		}
		if (weekList[1].checked) {
			clock2.unshift(1)
		} else {
			clock2.unshift(0)
		}
		if (weekList[2].checked) {
			clock3.unshift(1)
		} else {
			clock3.unshift(0)
		}
		let clockCode1 = ''
		let clockCode2 = ''
		let clockCode3 = ''
		clock1.forEach(item => {
			clockCode1 += item
		})
		clock2.forEach(item => {
			clockCode2 += item
		})
		clock3.forEach(item => {
			clockCode3 += item
		})
		clockCode1 = parseInt(clockCode1, 2).toString(16)
		clockCode2 = parseInt(clockCode2, 2).toString(16)
		clockCode3 = parseInt(clockCode3, 2).toString(16)
	
		code = (this.hexToDec(clockCode1) + this.hexToDec(clockCode2) + this.hexToDec(clockCode3))
		//处理时间
		let t1 = []
		let t2 = []
		let t3 = []

		weekList.forEach((item, index) => {
			if (index == 0) {
				if (item.time == '请选择时间') {
					t1 = [0, 0]
				} else {
					t1 = item.time.split(":")
				}
			}
			if (index == 1) {

				if (item.time == '请选择时间') {
					t2 = [0, 0]
				} else {
					t2 = item.time.split(":")
				}
			}
			if (index == 2) {
				if (item.time == '请选择时间') {
					t3 = [0, 0]
				} else {
					t3 = item.time.split(":")
				}
			}
		})
		console.log(code);
		let tcount = Number(t1[0]) + Number(t1[1]) + Number(t2[0]) + Number(t2[1]) + Number(t3[0]) + Number(t3[1])
		
		console.log(t1[0]);
		console.log(t1[1]);
		code = tcount + code+6
		 // % 256
		console.log(code);
		code=code%256
		code=code.toString(16)
		console.log(code);

		let a = ['0x55', '0xaa', '0x0a', '0x06', '0x' + clockCode1, '0x' + Number(t1[0]).toString(16), '0x' + Number(t1[1]).toString(16), '0x' + clockCode2, '0x' + Number(t2[0]).toString(16),
			'0x' + Number(t2[1]).toString(16), '0x' + clockCode3, '0x' + Number(t3[0]).toString(16), '0x' + Number(t3[1]).toString(16),
			'0x' + code, '0xff', '0xff', '0xff', '0xff', '0xff', '0xff']
		console.log(a);
		this.send(a)
	}
	// TODO 温度湿度设置 说明：校准参数是校准温度与实际温度的差值；正负值处理，小数部分最高位为 1 时，数据为负；为 0 时，数据为正；
	setTemperature(temperature, humidity) {
		let temperatureList=[]
		let isFuT=false
		let isFuH=false
		if (Number(temperature)>=0) {
			temperatureList = temperature.split('.')
			if (temperatureList.length == 1) {
				temperatureList.push(0)
			}
		}else{
			//处理负数
			temperature=temperature.replace(/\-/g,"")
			console.log(temperature);
			temperatureList = temperature.split('.')
			isFuT=true
			if (temperatureList.length == 1) {
				temperatureList.push(0)
			}
		}
		let humidityList =[]
		if (Number(humidity)>=0) {
			 humidityList = humidity.split('.')
			if (humidityList.length == 1) {
				humidityList.push(0)
			}
		}else{
			humidity=humidity.replace(/\-/g,"")
			humidityList = humidity.split('.')
			isFuH=true
			if (humidityList.length == 1) {
				humidityList.push(0)
			}
		}
		let code = 3
		code += Number(temperatureList[0])
		temperatureList[0]=temperatureList[0] = '0x' + (temperatureList[0].toString(16).padStart(2, '0'))
		if (isFuT) {
			temperatureList[1] = '0x8' + (temperatureList[1].toString(16))
		}else{
			temperatureList[1] = '0x' + (temperatureList[1].toString(16).padStart(2, '0'))
		}
		code += this.hexToDec(temperatureList[1])
		
		code += Number(humidityList[0])
		if (isFuH) {
			humidityList[1] = '0x8' + (humidityList[1].toString(16))
		}else{
			humidityList[1] = '0x' + (humidityList[1].toString(16).padStart(2, '0'))
		}
		code += this.hexToDec(humidityList[1])

		code = code.toString(16).padStart(2, '0')

		console.log(temperatureList);

		 let a = ['0x55', '0xaa', '0x05', '0x03', temperatureList[0], temperatureList[1], humidityList[0], humidityList[1], '0x' + code, '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff']

		this.send(a)
	}
	//时间设置
	setTime(dateTime) {
		let code = 0
		let times = dateTime.split(",")
		times.forEach((item, index) => {
			if (index == 0) {
				item = item.substring(2, 4)
			}
			code = code + Number(item)
			times[index] = '0x' + Number(item).toString(16).padStart(2, '0')
		})
		code = Number(code + 1).toString(16)
		let a = ['0x55', '0xaa', '0x07', ' 0x01', ...times, '0x' + code, ' 0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff']
		this.send(a)
	}

	//倒计时
	timeOut(min) {
		let num = Number(min).toString(16)

		let code = parseInt(8, 10) + Number(min)
		code = code.toString(16)
		min = '0x' + (num.padStart(2, '0'))

		let a = [0x55, 0xaa, 0x02, 0x08, min, '0x' + code, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
		console.log(a);
		this.send(a)
	}


	// 【5】停止搜索
	stopDiscovery() {
		uni.stopBluetoothDevicesDiscovery({
			success(res) {
				console.log('停止成功')
				console.log(res)
			},
			fail(err) {
				console.log('停止失败')
				console.error(err)
			}
		})
	}
	initBlue() {
		let that = this
		uni.openBluetoothAdapter({
			success(res) {
				console.log('初始化蓝牙成功')
				console.log(res)
				that.discovery()
				// setTimeout(() => {
				// 	that.connect()
				// }, 2000)
			},
			fail(err) {
				console.log('初始化蓝牙失败')
				console.error(err)
			}
		})
	}
	// // 【2】开始搜寻附近设备
	discovery() {
		console.log(this.blueDeviceList);
		let that = this
		console.log(that);
		uni.startBluetoothDevicesDiscovery({
			success(res) {
				console.log('开始搜索')
				console.log(res);
				// 开启监听回调
				console.log(res);
				uni.onBluetoothDeviceFound((devices) => {
					console.log(devices.devices[0]);
					// that.blueDeviceList.push(devices.devices[0])
					if(devices.devices[0].name == "HXDevice"){
						console.log(22222222222222222);
						that.blueDeviceList.push(devices.devices[0])
						console.log("000000",that.blueDeviceList);
					}
					let flag = true
					let name = devices.devices[0].name
					if (that.blueDeviceList.length) {
						that.blueDeviceList.forEach(item => {
							if (name == item.name && name.indexOf('不支持') == -1 && name != '') {
								flag = false
							}
						})
						if (flag && name != '') {
							console.log(devices.devices[0].name);
							that.blueDeviceList.push(devices.devices[0])
						}
					} else {
						if (devices.devices[0].name.indexOf('不支持') == -1 && name != '') {
							that.blueDeviceList.push(devices.devices[0])
						}
					}
					uni.setStorageSync("blueDeviceList",that.blueDeviceList);
				})
			},
			fail(err) {
				console.log('搜索失败')
				console.error(err)
			}
		})
	}
	// 【4】连接设备
	connect(data) {
		uni.showLoading({
			title: '连接中'
		})
		// this.deviceId = this.deviceId // 将获取到的设备ID存起来
		let that = this
		uni.createBLEConnection({
			// deviceId: this.deviceId,
			deviceId: data.deviceId,
			success(res) {
				uni.hideLoading()
				console.log('连接成功')
				console.log(res)
				// 停止搜索
				that.stopDiscovery()
				that.getServices()
				
				uni.showToast({
					title: '绑定成功'
				})
			},
			fail(err) {
				uni.hideLoading()
				that.stopDiscovery()
				console.log('连接失败')
				console.error(err)
				uni.showToast({
					title: '连接失败',
					icon: 'error'
				})
			}
		})
	}

	// 【5】停止搜索
	stopDiscovery() {
		uni.stopBluetoothDevicesDiscovery({
			success(res) {
				console.log('停止成功')
				console.log(res)
			},
			fail(err) {
				console.log('停止失败')
				console.error(err)
			}
		})
	}

	// 【6】获取服务
	getServices() {
		let that = this
		// 如果是自动链接的话，uni.getBLEDeviceServices方法建议使用setTimeout延迟1秒后再执行
		uni.getBLEDeviceServices({
			deviceId: this.deviceId,
			success(res) {
				uni.getBLEDeviceCharacteristics({
					// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
					deviceId: that.deviceId,
					// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
					serviceId: that.serviceId,
					success(rr) {
						that.characteristicId = rr.characteristics[0].uuid
						that.notify()
					},
					fail(ff) {
						console.error(ff)
						uni.showToast({
							title: '获取服务失败',
							icon: 'error'
						})
					}
				})
				console.log(res) // 可以在res里判断有没有硬件佬给你的服务
				uni.showToast({
					title: '获取服务成功'
				})
			},
			fail(err) {
				console.error(err)
				uni.showToast({
					title: '获取服务失败',
					icon: 'error'
				})
			}
		})
	}
	// 【8】开启消息监听
	notify() {
		console.log(this.redId);
		let that = this
		uni.notifyBLECharacteristicValueChange({
			deviceId: this.deviceId, // 设备id
			serviceId: this.serviceId, // 监听指定的服务
			characteristicId: this.notfiyid, // 监听对应的特征值
			state: true,
			success(res) {
				console.log(res)
			 return	that.listenValueChange() 
				uni.showToast({
					title: '已开启监听'
				})
			},
			fail(err) {
				console.error(err)
				uni.showToast({
					title: '监听失败',
					icon: 'error'
				})
			}
		})
	}
	// 【7】获取特征值
	getCharacteristics() {
		// 如果是自动链接的话，uni.getBLEDeviceCharacteristics方法建议使用setTimeout延迟1秒后再执行
		uni.getBLEDeviceCharacteristics({
			deviceId: this.deviceId,
			serviceId: this.serviceId,
			success(res) {
				console.log(res) // 可以在此判断特征值是否支持读写等操作，特征值其实也需要提前向硬件佬索取的
				uni.showToast({
					title: '获取特征值成功'
				})
			},
			fail(err) {
				console.error(err)
				uni.showToast({
					title: '获取特征值失败',
					icon: 'error'
				})
			}
		})
	}


	// 【11】读取数据
	read() {
		uni.readBLECharacteristicValue({
			deviceId: this.deviceId,
			serviceId: this.serviceId,
			characteristicId: this.notfiyid,
			success(res) {
				console.log(res)
				uni.showToast({
					title: 'read指令发送成功'
				})
			},
			fail(err) {
				console.error(err)
				uni.showToast({
					title: 'read指令发送失败',
					icon: 'error'
				})
			}
		})
	}

	// 【10】发送数据
	send(content) {
		console.log(content);
		return new Promise((rev, rej) => {
			let arr = content
			const buffer = new ArrayBuffer(arr.length)
			const dataView = new DataView(buffer)

			for (var i = 0; i < arr.length; i++) {
				dataView.setUint8(i, arr[i])
			}
			console.log(buffer);
			let o = {
				deviceId: this.deviceId,
				serviceId: this.serviceId,
				characteristicId: this.characteristicId,
			}
			console.log(o);
			uni.writeBLECharacteristicValue({
				deviceId: this.deviceId,
				serviceId: this.serviceId,
				characteristicId: this.characteristicId,
				value: buffer,
				success(res) {

					console.log(res)
					uni.showToast({
						title: 'write指令发送成功'
					})
					
					
					this.listenValueChange();
					rev(res)
				},
				fail(err) {

					console.error(err)
					uni.showToast({
						title: 'write指令发送失败',
						icon: 'error'
					})
					rej(err)
				}
			})
		})

	}


	// ArrayBuffer转16进度字符串示例
	ab2hex(buffer) {
		console.log(buffer);
		const hexArr = Array.prototype.map.call(
			new Uint8Array(buffer),
			function (bit) {
				return ('00' + bit.toString(16)).slice(-2)+','
			}
		)
		return hexArr.join('')
	}

	// 【9】监听消息变化
	listenValueChange() {
		uni.onBLECharacteristicValueChange(res => {
			console.log("99999999999999",res)
			let resHex = this.ab2hex(res.value)
			console.log(resHex)
			this.messageHex = resHex
			//处理设备发送的指令
			
			let result = this.hexCharCodeToStr(resHex)
			console.log(String(result))
			this.message = String(result)
			this.listenHandle(resHex,res)
		})
	}
	//处理设备发送的指令
	listenHandle(res,info){
		// res=let a=[55,aa,01,1b,1b,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff]
		// res=['55','aa','0b','22','00','00','04','05','12','27','19','04','39','05','bf','ff','ff','ff','ff','ff']
		// let arr=res
		console.log(info);
		let arr=res.split(",")
		
		// arr=	arr.splice(arr.length-1,1)
		console.log(arr[3]);
		if (arr[3].indexOf('22')!=-1) {
			// let arr=[ 55,'aa','0b',22,'00','00','03',14,13,'0a',16,'05',25,'09','9f',]
			 if (arr[11].substring(0,1)==8) {
			 	arr[10]='-'+arr[10]
			 	
			 }
			let text='报警记录为：'+this.hexToDec(arr[6])+'月'+
			this.hexToDec(arr[7])+'日'+
			this.hexToDec(arr[8])+'时'+
			this.hexToDec(arr[9])+'分温度为'+
			this.hexToDec(arr[10])+'.'+	this.fuNumToDoc(arr[11])+'湿度为'+
			this.hexToDec(arr[12])+'.'+	this.fuNumToDoc(arr[13])
			uni.$emit('listenHandle',{
				type:'读报警记录',
				day:this.hexToDec(arr[7]),
				hour:	this.hexToDec(arr[8]),
				min:this.hexToDec(arr[9]),
				temperature:this.hexToDec(arr[10])+'.'+this.fuNumToDoc(arr[11]),
				humidity:this.hexToDec(arr[12])+'.'+	this.fuNumToDoc(arr[13]),
				info:info
			})
			
		}
		if (arr[3].indexOf('26')!=-1) {
			console.log('读闹钟');
			//获取闹钟星期数
			
			let clo1=parseInt(arr[4],16).toString(2).padStart(8,'0').split('')
			console.log(clo1);
			let text='闹钟1'
			if (clo1[0]==0) {
				text+='关；开启时间'+this.hexToDec(arr[5])+"点"+this.hexToDec(arr[6])+'分'
			}else{
				text+='开；开启时间'+this.hexToDec(arr[5])+"点"+this.hexToDec(arr[6])+'分'
			}
			let week1=[]
		for (var i = 1; i <= 7; i++) {
			if (clo1[i]==1) {
				text+='每周'+(7-(i-1))
				week1.push(7-(i-1))
			}
		}
			let clo2=parseInt(arr[7],16).toString(2).padStart(8,'0').split('')
			console.log(clo2);
			let text2='闹钟2'
			if (clo2[0]==0) {
				text2+='关；开启时间'+this.hexToDec(arr[8])+"点"+this.hexToDec(arr[9])+'分'
			}else{
				text2+='开；开启时间'+this.hexToDec(arr[8])+"点"+this.hexToDec(arr[9])+'分'
			}
			let week2=[]
			for (var i = 1; i <= 7; i++) {
				if (clo2[i]==1) {
					text2+='每周'+(7-(i-1))
					week2.push(7-(i-1))
				}
			}
			let clo3=parseInt(arr[10],16).toString(2).padStart(8,'0').split('')
			console.log(clo3);
			let text3='闹钟3'
			if (clo3[0]==0) {
				text3+='关；开启时间'+this.hexToDec(arr[11])+"点"+this.hexToDec(arr[12])+'分'
			}else{
				text3+='开；开启时间'+this.hexToDec(arr[11])+"点"+this.hexToDec(arr[12])+'分'
			}
			let week3=[]
		for (var i = 1; i <= 7; i++) {
			if (clo3[i]==1) {
				text3+='每周'+(7-(i-1))
				week3.push(7-(i-1))
			}
		}
			console.log(text);
			console.log(text2);
			console.log(text3);
			uni.$emit('listenHandle',{
				type:'读闹钟',
				clo1:{
					week:week1,
					hour:	this.hexToDec(arr[5]),
					min:this.hexToDec(arr[6]),
					status:clo1[0]==0?false:true,
				},
				clo2:{
					week:week2,
					hour:	this.hexToDec(arr[8]),
					min:this.hexToDec(arr[9]),
					status:clo2[0]==0?false:true,
				},
				clo3:{
					week:week3,
					hour:	this.hexToDec(arr[11]),
					min:this.hexToDec(arr[12]),
					status:clo3[0]==0?false:true,
				},

			})
			
		}
		//温湿度上下限数据、亮度、背光灯常亮开关、报警开关、温湿度校准值上报
		if (arr[3].indexOf('62')!=-1) {
			console.log('温湿度上下限数据');
			//上限是负数
			let upFu=false
			if (arr[5].substring(0,1)==8) {
				arr[4]='-'+arr[4]
				
			}
			arr[5]=this.fuNumToDoc(arr[5])
			if (arr[9].substring(0,1)==8) {
				arr[8]='-'+arr[8]
				
			}
			//亮度 警报开关
			let liang=arr[12].substring(1,2)
			let off=arr[12].substring(0,1)
			off=parseInt(off,16)
			console.log(off);
			off=Number(off) .toString(2).padEnd(4,'0')
			console.log(off);
			let alarm=false
			let luminance=false
			
			if (off.substring(0,1)==1) {
				//警报开启
				alarm=true
			}
			if (off.substring(1,2)==1) {
				//警报开启
				luminance=true
			}
			arr[9]=this.fuNumToDoc(arr[9])
			let text='温度上限是'+this.hexToDec(arr[4])+'.'+arr[5]+'。温度下限是'+this.hexToDec(arr[8])+'.'+arr[9]+
			'湿度上限是'+this.hexToDec(arr[6])+'.'+this.hexToDec(arr[7])+
			'湿度下限是'+this.hexToDec(arr[10])+'.'+this.hexToDec(arr[11])+
			'亮度值为'+this.hexToDec(liang)+'亮度开关为：'+luminance+'警报开关为：'+alarm
			uni.showModal({
				title:text
			})
			uni.$emit('listenHandle',{
				type:'温湿度上下限数据',
				tup:this.hexToDec(arr[4])+'.'+arr[5],
				tlow:this.hexToDec(arr[8])+'.'+arr[9],
				hup:this.hexToDec(arr[6])+'.'+this.hexToDec(arr[7]),
				tlow:this.hexToDec(arr[10])+'.'+this.hexToDec(arr[11]),
				lnum:this.hexToDec(liang),
				lstatus:luminance,
				alarm:alarm,
				info:info

			})
			console.log(text);
		}
		
		if (arr[3]==14) {
			console.log('读温度湿度');
			if (!this.hexFixToDec(arr[11])) {
				arr[10]='-'+this.hexToDec(arr[10])
			}
			let text=Number(2000+Number(this.hexToDec(arr[4])))+'年'+this.hexToDec(arr[5])+'月'+this.hexToDec(arr[6])+'日'+this.hexToDec(arr[7])+'时'+
			this.hexToDec(arr[8])+'分'+
			this.hexToDec(arr[9])+'秒'+
			arr[10]+'.'+this.hexToDec(arr[11])+'温度'+
			this.hexToDec(arr[12])+'.'+this.hexToDec(arr[13])+'湿度'
			console.log(text);
			uni.$emit('listenHandle',{
				type:'读温度湿度',
				year:Number(2000+Number(this.hexToDec(arr[4]))),
				month:this.hexToDec(arr[5]),
				day:this.hexToDec(arr[6]),
				hour:this.hexToDec(arr[7]),
				min:this.hexToDec(arr[8]),
				sec:this.hexToDec(arr[9]),
				temperature:this.hexToDec(arr[10])+'.'+this.fuNumToDoc(arr[11]),
				humidity:this.hexToDec(arr[12])+'.'+	this.fuNumToDoc(arr[13]),
				info:info
			
			})
		}
		//7、历史数据上报
		if (arr[3]=='1a') {
			if (!this.hexFixToDec(arr[9])) {
				arr[8]='-'+this.hexToDec(arr[8])
			}
			if (!this.hexFixToDec(arr[13])) {
				arr[12]='-'+this.hexToDec(arr[12])
			}
			let text=this.hexToDec(arr[16])+'月'+Number(this.hexToDec(arr[4]))+'日'+this.hexToDec(arr[5])+'时'+this.hexToDec(arr[6])+'日'+this.hexToDec(arr[7])+'时'+

			arr[8]+'.'+   this.hexToDec(arr[9])+'温度'+
			this.hexToDec(arr[10])+'.'+this.hexToDec(arr[11])+'湿度。半点 温度'+arr[12]+'.'+this.hexToDec(arr[13])+'。湿度：'+this.hexToDec(arr[14])+'.'+this.hexToDec(arr[15])
			console.log(text);
			// uni.showToast({
			// 	title:text,
			// })
			
			uni.$emit('listenHandle',{
				type:'历史数据上报',
				
				month:this.hexToDec(arr[16]),
				day:this.hexToDec(arr[4]),
				hour:this.hexToDec(arr[5]),
				min:this.hexToDec(arr[8]),
				sec:this.hexToDec(arr[9]),
				temperature:arr[8]+'.'+   this.hexToDec(arr[9]),
				humidity:	this.hexToDec(arr[10])+'.'+this.hexToDec(arr[11]),
				halfTem:arr[12]+'.'+this.hexToDec(arr[13]),
				half:this.hexToDec(arr[14])+'.'+this.hexToDec(arr[15]),
				info:info
			
			})
		}
		
	}

	// 将16进制的内容转成我们看得懂的字符串内容
	hexCharCodeToStr(hexCharCodeStr) {
		var trimedStr = hexCharCodeStr.trim();
		var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
		var len = rawStr.length;
		if (len % 2 !== 0) {
			alert("存在非法字符!");
			return "";
		}
		var curCharCode;
		var resultStr = [];
		for (var i = 0; i < len; i = i + 2) {
			curCharCode = parseInt(rawStr.substr(i, 2), 16);
			resultStr.push(String.fromCharCode(curCharCode));
		}
		return resultStr.join("");
	}
	//16进制转换成数组
	hexToArray(arr) {
		arr = arr.replace(/\s*/g, "")
		arr = arr.split(',')
		let list = []
		arr.forEach(item => {
			item = '0x' + item
			list.push(item)
		})
		return list
	}
}

export default bluetooth